//
//  SSKAlertHelper.m
//  SSKKit
//
//  Created by LiuYing on 2017/10/17.
//  Copyright © 2017年 LiuYing. All rights reserved.
//

#import "SSKAlertHelper.h"

@implementation SSKAlertHelper


+ (void)showAlert:(NSString *)alertTitle
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
          handler:(void (^)(UIAlertAction * _Nonnull))handler {
    
    [SSKAlertHelper showAlert:alertTitle
                      message:message
                  cancelTitle:cancelTitle
                  otherTitles:nil
                      handler:handler];
    
}

+ (void)showAlert:(NSString *)alertTitle
          message:(NSString *)message
      cancelTitle:(NSString *)cancelTitle
      otherTitles:(NSArray<NSString *> *)otherTitles
          handler:(void (^)(UIAlertAction * _Nonnull))handler {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    for (NSString *title in otherTitles) {
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:handler];
        [alert addAction:action];
    }
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:handler];
    [alert addAction:action];
   
    UIViewController *viewController = [self topViewController];
    [viewController presentViewController:alert animated:YES completion:^{
        
    }];
}

+ (void)showSheet:(NSArray<NSString *> *)titles
      actionTitle:(NSString *)cancelTitle
          handler:(void (^)(UIAlertAction * _Nonnull))handler {
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSString *title in titles) {
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:handler];
        [sheet addAction:action];
    }

    // cancel
    UIAlertAction *action = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:handler];
    [sheet addAction:action];
    
    UIViewController *viewController = [self topViewController];
    [viewController presentViewController:sheet animated:YES completion:^{
        
    }];
    
}

// 模态内部  
+ (UIViewController *)topViewController {

    UIViewController *resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)viewController topViewController]];
    } else if ([viewController isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)viewController selectedViewController]];
    }
    return viewController;
}

@end
